import { createRouter, createWebHistory } from 'vue-router'

import { useUserStore } from '@/stores'

import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

NProgress.configure({ showSpinner: false })

const whiteList = ['/login', '/login/callback']

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      component: () => import('@/views/Login/index.vue'),
      meta: {
        title: '登录'
      }
    },
    {
      path: '/login/callback',
      component: () => import('@/views/Login/login-callback.vue'),
      meta: {
        title: '登录'
      }
    },
    {
      path: '/',
      component: () => import('@/views/TabBar-Pages/index.vue'),
      redirect: '/home',
      children: [
        {
          path: 'home',
          component: () => import('@/views/TabBar-Pages/Home/index.vue'),
          meta: {
            title: '首页'
          }
        },
        {
          path: 'article',
          component: () => import('@/views/TabBar-Pages/Article/index.vue'),
          meta: {
            title: '健康百科'
          }
        },
        {
          path: 'notify',
          component: () => import('@/views/TabBar-Pages/Notify/index.vue'),
          meta: {
            title: '消息通知'
          }
        },
        {
          path: 'user',
          component: () => import('@/views/TabBar-Pages/User/index.vue'),
          meta: {
            title: '个人中心'
          }
        }
      ]
    },
    {
      path: '/room',
      component: () => import('@/views/Room/index.vue'),
      meta: {
        title: '问诊室'
      },
      beforeEnter(to) {
        if (to.query.payResult === 'false') return '/user/consult'
      }
    },
    {
      path: '/user/patient',
      component: () => import('@/views/User-Pages/user-patient.vue'),
      meta: {
        title: '家庭档案'
      }
    },
    {
      path: '/user/consult',
      component: () => import('@/views/User-Pages/user-consult.vue'),
      meta: {
        title: '问诊记录'
      }
    },
    {
      path: '/user/consult/:id',
      component: () => import('@/views/User-Pages/user-detail.vue'),
      meta: {
        title: '问诊详情'
      }
    },
    {
      path: '/consult/fast',
      component: () => import('@/views/Consult-Pages/consult-fast.vue'),
      meta: {
        title: '极速问诊'
      }
    },
    {
      path: '/consult/dep',
      component: () => import('@/views/Consult-Pages/consult-dep.vue'),
      meta: {
        title: '选择科室'
      }
    },
    {
      path: '/consult/illness',
      component: () => import('@/views/Consult-Pages/consult-illness.vue'),
      meta: {
        title: '病情描述'
      }
    },
    {
      path: '/consult/pay',
      component: () => import('@/views/Consult-Pages/consult-pay.vue'),
      meta: {
        title: '问诊支付'
      }
    },
    {
      path: '/order/pay',
      component: () => import('@/views/Order-Pages/order-pay.vue'),
      meta: {
        title: '药品支付'
      }
    },
    {
      path: '/order/logistics/:id',
      component: () => import('@/views/Order-Pages/order-logistics.vue'),
      meta: {
        title: '订单物流'
      }
    },
    {
      path: '/order',
      component: () => import('@/views/Order-Pages/order-list.vue'),
      meta: {
        title: '订单记录'
      }
    },
    {
      path: '/order/:id',
      component: () => import('@/views/Order-Pages/order-detail.vue'),
      meta: {
        title: '订单详情'
      }
    },
    {
      path: '/order/pay/result',
      component: () => import('@/views/Order-Pages/order-result.vue'),
      meta: {
        title: '支付结果'
      },
      beforeEnter(to) {
        if (to.query.payResult === 'false') return `/order?status=10`
      }
    }
  ]
})

export const { push } = router

router.beforeEach((to) => {
  NProgress.start()
  const userStore = useUserStore()
  if (!userStore.user?.token && !whiteList.includes(to.path)) return '/login'
})

router.afterEach((to) => {
  NProgress.done()
  document.title = `优医问诊${to.meta?.title ? '-' + to.meta.title : ''}`
})

export default router
